package com.zzl.bonds.ticket.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.dto.ContractemplateAddDto;
import com.zzl.bonds.ticket.dto.ContractemplateUpdateDto;
import com.zzl.bonds.ticket.entity.ContractTemplateEntity;
import com.zzl.bonds.ticket.service.ContractemplateService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
/**
 * 合同模板
 *
 * @author sunpp
 * @date 2022-02-16 14:04:04
 */
@ApidocController("合同模板")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("bondsticket/contracttemplate")
@Slf4j
public class ContractTemplateController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private ContractemplateService contractemplateService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询合同模板数据", operationId = "/ticket/contracttemplate/list")
    @ApidocReturnPage(genericTypeClass = ContractTemplateEntity.class)
    public Result list(@ApidocParamsPage(name = "params") @RequestParam Map<String, Object> params){
        PageUtils page = contractemplateService.queryPage(params);
        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "通过id获取合同模板数据",operationId = "/ticket/contracttemplate/info")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "contracttemplate", valueClass = ContractTemplateEntity.class)
    })
    public Result info(@ApidocParam(name = "合同模板id", required = true) @PathVariable("id") String id){
		ContractTemplateEntity contractemplate = contractemplateService.getById(id);
        return Result.ok().put("contractemplate", contractemplate);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "保存合同模板",operationId = "/ticket/contractemplate/save")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的合同模板对象值", required = true) @RequestBody ContractemplateAddDto contractemplate){
        try {
            contractemplateService.saveContractemplate(contractemplate, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApidocOperation(description = "修改合同模板",operationId = "/ticket/contractemplate/update")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的合同模板对象值", required = true) @RequestBody ContractemplateUpdateDto contractemplate){
        try {
            contractemplateService.updateContractemplateById(contractemplate, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除合同模板",operationId = "/ticket/contractemplate/delete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的合同模板对象id集合", required = true) @RequestBody String[] ids){
		contractemplateService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
